package com.wangsong.service;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class TestService {

    @Autowired
    private RedissonClient redissonClient;


    public boolean test(String id) {
        if (Objects.equals(id, "138")) {
            return true;
        }
        return false;
    }

    @Async
    public String productsCache() throws InterruptedException {

        RReadWriteLock productsLock = redissonClient.getReadWriteLock("productsLock");
        RBucket<String> products1;
        try {
            productsLock.readLock().lock();
            products1 = redissonClient.getBucket("productsBucket");
            if (products1.isExists()) {
                System.out.println(Thread.currentThread().getId() + "缓存第一次检测");
                return products1.get();
            }
        } finally {
            productsLock.readLock().unlock();
        }

        try {
            productsLock.writeLock().lock();
            products1 = redissonClient.getBucket("productsBucket");
            if (products1.isExists()) {
                System.out.println(Thread.currentThread().getId() + "缓存第二次检测");
                return products1.get();
            }
            String test = "test";
            products1.set(test);
            System.out.println(Thread.currentThread().getId() + "数据库取值");
            return test;
        } finally {
            productsLock.writeLock().unlock();

        }


    }

    public void productsCacheDelete() {
        RReadWriteLock productsLock = redissonClient.getReadWriteLock("productsLock");
        try {
            productsLock.writeLock().lock();
            RBucket<Object> products1 = redissonClient.getBucket("productsBucket");
            products1.delete();
        } finally {
            productsLock.writeLock().unlock();
        }
    }


    public void productsStock(Integer integer) {
        RAtomicLong productsAtomicLong = redissonClient.getAtomicLong("productsAtomicLong");
        productsAtomicLong.getAndAdd(integer);
    }

    public long productsStockDecrement() {
        RLock lock = redissonClient.getLock("productsStockLock");
        try {
            lock.lock();
            RAtomicLong productsAtomicLong = redissonClient.getAtomicLong("productsAtomicLong");
            long l = productsAtomicLong.get();
            if (l == 0) {
                return 0L;
            }
            return productsAtomicLong.getAndDecrement();
        } finally {
            lock.unlock();
        }
    }

    public long productsStockIncrement() {
        RLock lock = redissonClient.getLock("productsStockLock");
        try {
            lock.lock();
            RAtomicLong productsAtomicLong = redissonClient.getAtomicLong("productsAtomicLong");
            return productsAtomicLong.getAndIncrement();
        } finally {
            lock.unlock();
        }
    }
}
